package com.shire.shire_interface.service.account.impl;

import com.alibaba.fastjson.JSONObject;
import com.shire.shire_interface.controller.kf.WebSocketController;
import com.shire.shire_interface.dao.accounts.UserAccountDao;
import com.shire.shire_interface.dao.accounts.WxAccountDao;
import com.shire.shire_interface.model.accounts.UserAccount;
import com.shire.shire_interface.model.accounts.UserAccountExample;
import com.shire.shire_interface.model.command.WxCommand;
import com.shire.shire_interface.model.user.WxAccount;
import com.shire.shire_interface.model.user.WxAccountExample;
import com.shire.shire_interface.service.account.WxAccountService;
import org.springframework.cache.annotation.CacheConfig;
import org.springframework.stereotype.Service;

import javax.annotation.Resource;
import java.util.List;

@CacheConfig(cacheNames = {"myCache"})
@Service
public class WxAccountServiceImpl implements WxAccountService {

    @Resource
    private WxAccountDao wxAccountDao;

    @Resource
    private UserAccountDao userAccountDao;


    @Override
//    @Cacheable(key = "targetClass + methodName +#p0")
    public WxAccount getWxAccountById(Integer accountId) {
        return wxAccountDao.selectByPrimaryKey(accountId);
    }

    @Override
//    @Cacheable(key = "targetClass + methodName +#p0 +#p1")
    public WxAccount getWxAccountByAccountName(Integer csId, String account) {
        WxAccountExample example = new WxAccountExample();
        example.createCriteria().andWxidEqualTo(account);

        List<WxAccount> accountList = wxAccountDao.selectByExample(example);
        if (accountList != null && accountList.size() > 0)
            return  accountList.get(0);
        return null;
    }

    @Override
//    @Cacheable(key = "targetClass + methodName +#p0")
    public WxAccount getWxAccountByWxId(String wxId) {
        WxAccountExample example = new WxAccountExample();
        example.createCriteria().andWxidEqualTo(wxId);

        example.or().andAliasEqualTo(wxId);
        List<WxAccount> accountList = wxAccountDao.selectByExampleWithBLOBs(example);
        if (accountList != null && accountList.size() > 0)
            return  accountList.get(0);
        return null;
    }

    @Override
    public List<UserAccount> getSystemUserByWxid(String wxId) {
        UserAccountExample example = new UserAccountExample();
        example.createCriteria().andWxAccountEqualTo(wxId);
        return userAccountDao.selectByExample(example);
    }


    @Override
    public int insertWxAccount(WxAccount wxAccount) {
        return wxAccountDao.insertSelective(wxAccount);
    }

    @Override
    public int updateWxAccount(WxAccount wxAccount) {
        return wxAccountDao.updateByPrimaryKeyWithBLOBs(wxAccount);
    }

    @Override
    public boolean isOneOfCs(String wxId) {
        WxAccountExample example = new WxAccountExample();
        example.createCriteria().andWxidEqualTo(wxId);
        List<WxAccount> accounts = wxAccountDao.selectByExample(example);
        return accounts != null && accounts.size() == 1;
    }

    @Override
    public List<WxAccount> getWxAccountsByCsId(WxAccountExample example) {

        return wxAccountDao.selectByExample(example);
    }

    @Override
    public void accountUpdated(WxCommand command) {
        WxAccount account = getWxAccountById(command.getAccountid());
        JSONObject syncWs = new JSONObject();
        syncWs.put("opType",8);
        syncWs.put("wxAccount", account.getWxid());
        syncWs.put("friendChanged", 1);

        List<UserAccount> list = getSystemUserByWxid(account.getWxid());
        for(UserAccount acc : list) {
            WebSocketController.sendMessage(acc.getUserId() + "", syncWs.toJSONString());
        }
    }

}
